package com.peanut.baby.mvp.task;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.peanut.baby.InitManager;
import com.peanut.baby.R;
import com.peanut.baby.db.DBManager;
import com.peanut.baby.model.Task;
import com.peanut.baby.model.TaskDBRecord;
import com.peanut.devlibrary.adapter.HolderAdapter;
import com.peanut.devlibrary.util.TimeUtil;

import java.util.List;

/**
 * Created by anonymouslystudio on 2017/8/9.
 */

public class TaskListAdapter extends HolderAdapter<Task,TaskListAdapter.Holder>{
    private static final String TAG = "TaskListAdapter";
    private TaskDBRecord record;
    public TaskListAdapter(Context context, List<Task> listData) {
        super(context, listData);

        if(InitManager.getInstance().getUser() != null){
            record = DBManager.getInstance().getTaskRecord(Integer.parseInt(InitManager.getInstance().getUserId()), TimeUtil.getCurrentDate());
        }
    }

    @Override
    public View buildConvertView(LayoutInflater layoutInflater, Task task, int position) {
        return inflate(R.layout.layout_item_task);
    }

    @Override
    public Holder buildHolder(View convertView, Task task, int position) {
        Holder  holder = new Holder();
        holder.name = (TextView) convertView.findViewById(R.id.task_name);
        holder.value = (TextView) convertView.findViewById(R.id.task_value);
        holder.action = (TextView) convertView.findViewById(R.id.task_action);
        return holder;
    }

    @Override
    public void bindViewDatas(Holder holder, Task task, int position) {
        holder.name.setText(task.name);
        holder.value.setText(task.value);
        holder.action.setText(task.action);

        holder.value.setText(task.value+"");

        if(isTaskFinished(task)){
            holder.action.setText("已完成");
            holder.action.setEnabled(false);
        }else{
            holder.action.setText(task.action);
            holder.action.setEnabled(true);
        }


        String displayName = task.name;
        if(task.type == Task.TYPE_NEW_MOMENT){
            displayName += record != null? "("+record.sendMomentCount+"/"+Task.MAX_NEW_MOMENTS+")" : "(0/"+Task.MAX_NEW_MOMENTS+")";
        }else if(task.type == Task.TYPE_NEW_REPLY){
            displayName += record != null? "("+record.replyMomentCount+"/"+Task.MAX_REPLY_MOMENTS+")" : "(0/"+Task.MAX_REPLY_MOMENTS+")";
        }
        holder.name.setText(displayName);
    }

    class Holder{
        TextView    name;
        TextView    value;
        TextView    action;
    }

    private boolean isTaskFinished(Task task){
        if(record == null){
            return false;
        }
        boolean flag = false;
        switch (task.type){
            case Task.TYPE_NEW_MOMENT:
                flag = record.sendMomentCount >= Task.MAX_NEW_MOMENTS;
                break;
            case Task.TYPE_NEW_REPLY:
                flag = record.replyMomentCount >= Task.MAX_REPLY_MOMENTS;
                break;
            case Task.TYPE_SHARE_MOMENT:
                flag = record.shareMomentCount >= 1;
                break;
            case Task.TYPE_SHARE_QA:
                flag = record.shareQACount >= 1;
                break;
            case Task.TYPE_SHARE_LIVE:
                flag = record.shareLiveCount >= 1;
                break;
            case Task.TYPE_SHARE_APP:
                flag = record.shareAppCount >= 1;
                break;
        }
        return flag;

    }
}
